<style lang="less" scoped>
.cf {
  clear: both;
}

.noDate_page {
  padding-top: 2rem;
}
</style>

<!--司机端提货签收列表页-->
<template>
  <div class="gridContainer">
    <!-- <manage-header></manage-header>
    <header-car :status="4"></header-car> -->
    <Search :results="results"
            v-model="billno"
            position="absolute"
            auto-scroll-to-top
            @on-submit="onSubmit"
            @on-cancel="OnCancel"
            ref="search"></Search>
    <scroller lock-x
              scrollbar-y
              use-pullup
              :height="scrollerHeight"
              v-if="totalRecords > 0"
              :pullup-config="ScrollerConfig"
              ref="Scroller">
      <div>
        <div class="acpt-list"
             v-for="(item, index) in searchData">

          <div class="car-pc-title cf listing">
            <span class="f-l"><i class="llse llse-huoche"></i> <b class="atvno">{{item.ATVNo}}</b></span>
            <!-- <em class="f-r"
                v-if="item.PlanRouteState<15">
              <div class="btn ml10"
                   @click="DriverDeparture(item.Driver1Guid,item.VehicleGuid,item.ATVGuid)">发车</div>
            </em> -->
            <!-- <em class="f-r"><div class="btn ml10" @click="transferAll(item.ATVGuid)">整车中转</div></em> -->
          </div>

          <div class="acpt-list">
            <div class="acpt-list-wrap"
                 v-for="(bbitem, index) in item.list"
                 :class="index+1 === item.list.length?'mb30':'lineb-dash'">
              <div class="acpt-list-head cf">
                <span class="txt f-l">业务单号：{{bbitem.BBillNo}}</span>
              </div>
              <div class="displayflex acpt-list-body lineb">
                <span class="cargo">
                  <i class="iconfont icon-cargo"></i>
                  <p>{{bbitem.Pkgs}}件</p>
                </span>
                <div class="txt flex1">
                  <p class="ft17">{{bbitem.PickContacts}} {{bbitem.PickContactsTel}}</p>
                  <p>取货地址：{{bbitem.DepartureCity | city}}{{bbitem.DepartureDistrict | district}}{{bbitem.DepartureAddress}}</p>
                  <p>送货地址：{{bbitem.ArrivalCity | city}}{{bbitem.ArrivalDistrict | district}}{{bbitem.ArrivalAddress}}</p>
                </div>
              </div>
              <div class="btn-grp txt-rit">
                <div class="btn ml10"
                     @click="viewOrderDetail(bbitem.BBillGuid)">详情</div>
                <div class="btn ml10"
                     @click="goDelivery(bbitem.BBillNo, bbitem.AtvGuid)">交货</div>
                <div class="btn ml10"
                     @click="viewOrderJs(bbitem.BBillGuid)">拒签</div>
                <!-- <div class="btn ml10"
                     v-if="bbitem.IsCCBB =='Y'"
                     @click="carLlbackOn(bbitem.BBillNo)">温度打印</div> -->
                <!-- <div class="btn ml10" @click="transfer(bbitem.Guid, bbitem.RowNo)">物流中转</div> -->
              </div>
            </div>
          </div>
        </div>
      </div>
    </scroller>

    <div class="noDate_page"
         v-if="totalRecords == 0">
      <div class="noDate_icon"><i class="iconfont icon-null"></i></div>
      暂无数据，请通知所属公司派单！
    </div>
    <div class="w100fix"></div>
    <footer-menu :status='2'></footer-menu>
  </div>
</template>

<script>
import manageHeader from "@/components/core/header/manage.header.vue";
import headerCar from '@/components/core/header/driver-progress.vue'
import footerMenu from "@/components/core/footer/manage.footer.vue";
import {
  mapGetters,
  mapState,
} from "vuex";
import { Search, Group, Cell, XButton } from 'vux'
//import { Search } from 'mint-ui';

export default {
  components: {
    footerMenu,
    headerCar,
    manageHeader,
    Search,
    Group,
    Cell,
    XButton
  },
  data () {
    return {
      datas: [{
        ATVNo: '',
        AtvGuid: '',
        list: [],
      }],
      BBillGuid: '',
      searchData: [],
      billno: '',
      totalRecords: 0,
      noMore: false,
      ScrollerConfig: {
        content: "",
        downContent: "",
        height: 0,
        loadingContent: "",
        upContent: ""
      },
      results: [],
      Memo: '',
    }
  },
  computed: {
    ...mapState({
      pagetitle: state => state.cfg.appBarTitle,
      showstate: state => state.cfg.showAppBar,
    }),
    ...mapGetters({
      scrollerHeight: "cfg/getConHeight"
    }),
    userinfo () {
      return localStorage.userinfo ? JSON.parse(localStorage.userinfo) : null;
    }
  },
  created () {
    this.getData();
  },
  mounted () {
    this.$store.dispatch("cfg/setConHeight");
  },
  methods: {
    getData () {
      let query = {
        DriverGuid: this.userinfo.StaffID,
        State: "52",
        ATVGuid: this.$route.query.Guid,
        SaleCorpName: this.$route.query.SaleCorpName,
      };
      this.Ajax.post("GetDriverOrderList", query).then(resp => {
        if (resp.IsSuccess) {
          this.totalRecords = resp.TotalRecords;
          if (resp.Datas && resp.Datas.length > 0) {
            let map = {},
              args = [];
            resp.Datas.forEach((x) => {
              if (!map[x.ATVNo]) {
                args.push({
                  ATVNo: x.ATVNo,
                  ATVGuid: x.AtvGuid,
                  Driver1Guid: x.Driver1Guid,
                  VehicleGuid: x.VehicleGUID,
                  PlanRouteState: x.PlanRouteState,
                  list: [x]
                });
                map[x.ATVNo] = x;
              } else {
                for (var i = 0; i < args.length; i++) {
                  if (args[i].ATVNo == x.ATVNo) {
                    args[i].list.push(x);
                    break;
                  }
                }
              }
            })
            args = args.sort(by("ATVNo"));
            this.datas = args;
            this.searchData = args;
            console.log(this.datas);
          }
        } else {
          this.Toast({
            message: "列表为空，请稍后刷新"
          });
        }
      });

      let by = function (name) {
        return function (o, p) {
          var a, b;
          if (typeof o === "object" && typeof p === "object" && o && p) {
            a = o[name];
            b = p[name];
            if (a === b) {
              return 0;
            }
            if (typeof a === typeof b) {
              return a < b ? -1 : 1;
            }
            return typeof a < typeof b ? -1 : 1;
          } else {
            throw ("error");
          }
        }
      };
    },

    transferAll (guid) {
      this.$router.push({
        path: '/driver/transferAll',
        query: {
          Guid: guid
        }
      });
    },

    transfer (guid, rowNo) {
      this.$router.push({
        path: '/driver/transfer',
        query: {
          Guid: guid,
          RowNo: rowNo
        }
      });
    },
    DriverDeparture (Driver1Guid, VehicleGuid, AtvGuid) {
      let query = {
        Driver1Guid: Driver1Guid,
        VehicleGuid: VehicleGuid,
        Guid: AtvGuid
      };
      console.log(query);
      this.Ajax.post("DriverDeparture", query).then(resp => {
        if (resp.IsSuccess) {
          this.datas.forEach(item => {
            if (item.ATVGuid == AtvGuid) {
              item.PlanRouteState = '15';
            }
          });
          this.Toast({
            message: "发车成功"
          });
        } else {
          this.Toast({
            message: "发车失败" + resp.ErrorMessage
          });
        }
      });
    },
    viewOrderDetail (BBillGuid) {
      this.$router.push({
        path: '/driver/senddingdetailorder',
        query: {
          Guid: BBillGuid,
        }
      });
    },

    carLlbackOn (BBillNo) {
      this.$router.push({
        path: '/driver/carColdTracking',
        query: {
          BBillNo: BBillNo,
        }
      });
    },

    goDelivery (BBillNo, AtvGuid) {
      this.$router.push({
        path: 'carDeliveryCheck',
        query: {
          BBillNo: BBillNo,
          AtvGuid: AtvGuid,
        }
      });
    },
    resultClick (item) {
      window.alert('you click the result item: ' + JSON.stringify(item))
    },
    onSubmit () {
      this.$refs.search.setBlur()
      var self = this;
      if (!self.billno) {
        self.totalRecords = 0;
        self.searchData = [];
        return;
      } else {
        var searchList = [];
        var data = this.datas[0];
        self.searchData[0].ATVNo = data.ATVNo;
        self.searchData[0].ATVGuid = data.ATVGuid;
        self.searchData[0].Driver1Guid = data.Driver1Guid;
        self.searchData[0].PlanRouteState = data.PlanRouteState;
        self.searchData[0].VehicleGuid = data.VehicleGuid;
        self.searchData[0].list = data.list.filter(item => { return item.BBillNo.indexOf(this.billno.trim()) >= 0 });

      }
    },
    OnCancel () {
      this.getData();
      // this.$set(this.searchData, this.datas)
    },
    viewOrderJs (BBillGuid) {
      this.MessageBox.prompt('请输入拒单原因！').then(({ value, action }) => {
        this.popupVisible = true;
        this.BBillGuid = BBillGuid;
        this.Memo = value;
        this.handleRefuse();
      });

    },
    handleRefuse () {
      var ReceiverID = this.userinfo.StaffID;
      var ReceiverName = this.userinfo.Name;
      var PRContactsTel = this.userinfo.MobileNo;
      var signName = this.userinfo.Name;
      let query = {
        Guid: this.BBillGuid,
        Receiver: ReceiverID,
        ReceiverName: signName,
        PRContactsTel: PRContactsTel,
        Memo: this.Memo,
        ExpReason: this.Memo,
      };
      this.Ajax.post("RefusalreasonConfirm", query).then(resp => {
        if (resp.IsSuccess) {
          this.$vux.toast.show({
            text: '拒签成功',
            type: 'success',
            onHide () {
              location.reload();
            }
          });

        } else {
          this.Toast({
            message: "拒签失败" + resp.ErrorMessage
          });
        }
      });
    },
  }
}
</script>
